﻿using System;
using System.Linq;
using System.Reflection;
using Microsoft.EntityFrameworkCore;

namespace DB.Context
{
    public class ApplicationSqlServerContext : BaseContext
    {
        public ApplicationSqlServerContext(DbContextOptions<ApplicationSqlServerContext> options)
            : base(options)
        {

        }
        protected override void OnConfiguring(DbContextOptionsBuilder builder)
        {

            //添加数据库连接字符串
            builder.UseSqlServer(@"Server=.;User id=sa;Password=Ilove0.618;Database=BlogDbContext");

        }
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            //添加FluentAPI配置
            var typesToRegister = Assembly.GetExecutingAssembly().GetTypes().Where(q => q.GetInterface(typeof(IEntityTypeConfiguration<>).FullName) != null);
            foreach (var type in typesToRegister)

            {
                dynamic configurationInstance = Activator.CreateInstance(type);
                builder.ApplyConfiguration(configurationInstance);
            }
        }
    }
}
